package com.eightywork.life.activity;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.framework.ShareSDK;

import com.eightywork.life.R;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.CacheService;
import com.eightywork.life.service.CpService;
import com.eightywork.life.service.DataService;
import com.eightywork.life.service.DoService;
import com.eightywork.life.service.SpaceService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;

public class StartActivity extends BaseActivity {

	private AsyncTask<Void, Void, String> task;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		task = new TagTask().execute();
	}

	@Override
	protected void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		super.onDestroy();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
	    JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}


	class TagTask extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			try {
				
				if (CacheService.isLogined()) {
					CacheService.saveProfile(CpService.profile());
				}	
				DataService.setTagList(SpaceService.tagList()); //获取分类接
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			if (CacheService.isRookie()) {
				Intent intent = new Intent(context, GuideActivity.class); //进入引导页
				startActivity(intent);
			} else {		
				if (CacheService.isLogined()) {
					if(!isConnectingToInternet()){
						Intent intent = new Intent(context, LoginActivity.class);
						intent.putExtra("failConnectToInternet", true);
						startActivity(intent);
					} else {
						Intent intent = new Intent(context, IndexActivity.class);
						intent.putExtra("beginToload", true);
						startActivity(intent);
						new DataTask().execute();
					}
				
				} else {
					Intent intent = new Intent(context, LoginMapActivity.class);
					startActivity(intent);
				}
			} 
			finish();	
			super.onPostExecute(result);
		}
	}
	
	public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }
 
          }
          return false;
    }

	class DataTask extends AsyncTask<Void, Void, String> {
		@Override
		protected String doInBackground(Void... params) {
			try {
				DataService.setStruMap(DoService.xiaoquStru()); //小区物业分布信息（期、栋、层、房号）
				DataService.setGroupList(SpaceService.group()); //好友分组
			} catch (MyHttpException e) {
				e.printStackTrace();
			}
			return null;
		}
	}
}
